<template>
  <section class="component pause-apply">
    <el-form
      :model="dataForm"
      :rules="rules"
      label-width="122px"
      ref="dataFrom"
    >
      <el-form-item
        label="停催到期日期"
        prop="casePauseDueDate"
        v-if="applyType === 'PAUSE_CASE'"
      >
        <el-date-picker
          :picker-options="$common.datePickDisabledDate(1)"
          @change="pauseDueTimeChange"
          placeholder="请选择日期"
          type="date"
          v-model="dataForm.casePauseDueDate"
          value-format="yyyy-MM-dd 23:59:59"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="申请原因" prop="applyDescription">
        <el-input
          :maxlength="200"
          :rows="3"
          type="textarea"
          v-model.trim="dataForm.applyDescription"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>

    <div class="operate-buttons">
      <el-button @click="emitClose">取消</el-button>
      <el-button :loading="submitLoading" @click="commit">确定</el-button>
    </div>

    <!-- 检查 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :visible.sync="dialog"
      append-to-body
      title="检查结果"
      width="800px"
    >
      <check-apply
        :applyType="applyType"
        :messageData="checkMessages"
        @close="cancelContinue()"
        @success="
          dialog = false;
          confirmSubmit();
        "
      ></check-apply>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import { Vue, Component, Emit, Prop } from "vue-property-decorator";
import CheckApply from "~/components/visit-case/check-apply.vue";
import { Dependencies } from "~/core/decorator";
import { FlowTaskService } from "~/services/domain-service/flow-task.service";
import { Form } from "element-ui";
import { CasePauseActiveService } from "~/services/domain-service/case-pause-active.service";
import { CaseApplyRecordService } from "~/services/domain-service/case-apply-record.service";
import { ApplyType } from "~/config/enum.config";
import { namespace } from "vuex-class";
import store from "~/store";
const CollectionExecutionModule = namespace("collection-execution");

@Component({
  components: {
    CheckApply
  }
})
export default class PauseApply extends Vue {
  @Dependencies(CaseApplyRecordService)
  private CaseApplyRecordService: CaseApplyRecordService;
  @Dependencies(FlowTaskService)
  private flowTaskService: FlowTaskService;
  @Dependencies(CasePauseActiveService)
  private CasePauseActiveService: CasePauseActiveService;
  @CollectionExecutionModule.State private caseNo;
  @CollectionExecutionModule.State private caseId;
  @CollectionExecutionModule.State private hiddenAction

  @Prop({ type: String }) private applyType;
  @Prop({ type: Array, required: true }) private caseIds;
  @Prop({ type: Object }) private otherData;

  private dataForm: any = {
    confirm: 0,
    caseIds: [],
    applyCategory: "",
    applyDescription: "",
    casePauseDueDate: ""
  };
  private checkMessages: any = {};
  private rules: any = {
    applyDescription: { required: true, message: "请输入申请原因" },
    casePauseDueDate: { required: true, message: "请选择停催到期日期" }
  };
  private submitLoading: boolean = false;
  private dialog: boolean = false;
  private perDueDate;

  @Emit("close") private emitClose() {
    this.resetDate();
  }

  private pauseDueTimeChange() {
    const currentDay: any = new Date(this.dataForm.casePauseDueDate);
    const nextDay: any = new Date();
    if (nextDay.getDate() > this.perDueDate) {
      nextDay.setMonth(nextDay.getMonth() + 1);
    }
    nextDay.setDate(this.perDueDate);
    if (currentDay > nextDay) {
      this.$message.warning("当前选择日期超过下个账单日");
    }
  }

  /**
   * 提交
   */
  private commit() {
    const dataFrom: any = this.$refs["dataFrom"];
    dataFrom.validate(valid => {
      if (!valid) return false;
      // this.commitNext();
      this.submit();
    });
  }

  /**
   * 检查案件
   */
  // private commitNext() {
  //   this.flowTaskService
  //     .checkApplyCategory({ applyCategory: this.applyType })
  //     .subscribe(data => {
  //       // 0 拒绝，1 提示，2 通过
  //       if (data.status === 1) {
  //         this.$confirm(
  //           "该" +
  //             this.$dict.getDictName(this.applyType, "ApplyCategory") +
  //             "申请将直接生效，是否继续?",
  //           "提示",
  //           {
  //             confirmButtonText: "确定",
  //             cancelButtonText: "取消",
  //             type: "warning"
  //           }
  //         )
  //           .then(() => {
  //             this.submit();
  //           })
  //           .catch(this.error);
  //       } else if (data.status === 2) {
  //         this.submit();
  //       }
  //     }, this.error);
  // }

  /**
   * 提交接口调用
   */
  private submit() {
    this.submitLoading = true;
    this.dataForm.caseIds = this.caseIds;
    this.dataForm.applyCategory = this.applyType;

    if (this.applyType === ApplyType.pause.value) {
      this.pause();
    } else {
      this.active();
    }
  }

  /**
   * 暂停催收
   */
  private pause() {
    this.dataForm.caseNumber = this.caseNo
    let requestModel = this.dataForm;
    if (this.otherData) {
      requestModel = { ...this.dataForm, ...this.otherData, isPauseOrActive: 'PAUSE_CASE' };
    }
    this.CasePauseActiveService.casePauseActive(requestModel).subscribe(this.success, this.error);
  }

  /**
   * 停催激活
   */
  private active() {
    this.CaseApplyRecordService
      .applyPauseCaseActive(this.dataForm)
      .subscribe(this.success, this.error);
  }

  /**
   * 申请成功
   */
  private success(data: any) {
    if (data && data.status !== 2) {
      this.checkMessages = data;
      this.dialog = true;
    } else {
      this.submitLoading = false;
      this.$message.success("操作成功");
      // store.commit('collection-execution/updateHiddenAction', true)
      this.emitClose();
    }
  }

  /**
   * 失败
   */
  private error() {
    this.submitLoading = false;
  }

  /**
   * 取消
   */
  private cancelContinue() {
    this.dialog = false;
    this.submitLoading = false;
  }

  /**
   * 继续提交
   */
  private confirmSubmit() {
    this.dataForm.confirm = 1;
    this.submit();
  }

  /**
   * 数据重置
   */
  private resetDate() {
    this.dataForm = {
      confirm: 0,
      caseIds: [],
      applyCategory: "",
      applyDescription: "",
      casePauseDueDate: ""
    };

    const form: any = this.$refs["dataFrom"] as Form;
    form.resetFields();

    this.checkMessages = {};
  }
}
</script>

<style lang="less"></style>
